package com.ynny.jdbc01;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;

public class JDBCDemo04 {

    public static void main(String[] args) throws SQLException {
        Driver driver = new Driver();
        DriverManager.registerDriver(driver);
        String url = "jdbc:mysql://localhost:3306/db01";
        String userName = "root";
        String password ="root";
        Connection connection = DriverManager.getConnection(url, userName, password);

        //4.得到PreparedStatement
        // 4.1携带参数
        //    ?  占位符

        String sql = "insert into book(id,name) values (?,?)";

        PreparedStatement preparedStatement =
                connection.prepareStatement(sql);

        //  4.2给占位符赋值
        //     parameterIndex 参数的下标 ----> 从1开始
             preparedStatement.setString(1,"15");
             preparedStatement.setString(2,"焦太郎");
//             preparedStatement.setString(2,"小灰灰");

        //5.执行sql语句 ----->查询 用的是 executeQuery
        //             ----->删除 添加 修改
        int i = preparedStatement.executeUpdate();


        System.out.println(i);



        //7.关闭连接

        preparedStatement.close();
        connection.close();


    }
}
